﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using ZLCMS.Web.Wap.Code;

namespace ZLCMS.Web.MemberUser
{
    public partial class ModifyUserInfo : WebMemberPageBase
    {
        public MemberInfo _memberDb;

        protected void Page_Load(object sender, EventArgs e)
        {
            _memberDb = Master.LoadMember();
            MemberCategory1.MenuIndex = "xgzl";
            //初始化全局变量值
          //  InitializeGlobalValue();
            //初始化控件值
            InitializeContent();
        }

        private void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                tb_name.Text = _memberDb.MemberName;
                tb_nickname.Text = _memberDb.NickName;
                tb_email.Text = _memberDb.Email;
                tb_phone.Text = _memberDb.Telephone;
                ta_des.Value = _memberDb.Introduction;
            }
        }

        /// <summary>
        /// 修改会员信息
        /// </summary>
        private void SaveUser()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ITransaction tran = session.BeginTransaction();
                ClientScriptManager csm = this.Page.ClientScript;
                try
                {
                    if (_memberDb.AccountId == null)
                    {
                        int _count = 0;

                        _count = Convert.ToInt32(session.CreateQuery("from MemberInfo where  AccountId=:acc or CompanyCode=:acc or Email= :acc ")
                        .SetParameter("acc", tb_name.Text).UniqueResult<MemberInfo>());

                        if (_count > 0)
                        {
                            throw new Exception("对不起,此帐号已被占用.");
                        }
                    
                    }   
                        _memberDb.MemberName = tb_name.Text;
                    _memberDb.NickName = tb_nickname.Text;
                    _memberDb.Email = tb_email.Text;
                    _memberDb.Telephone = tb_phone.Text;
                    _memberDb.Introduction = ta_des.Value;
                    session.Update(_memberDb);
                    tran.Commit();
                }
                catch (System.Exception ex)
                {
                    tran.Rollback();
                    csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('" + ex.Message + "');", true);
                }
            }
        }

        private void InitializeGlobalValue()
        {
           //if (!IsPostBack)
           // {
           //     ViewState["uid"] = Session[GlobalSessionString.CurrentMemberAccount];
           // }
           // _uid = (string)ViewState["uid"];

           // if (!string.IsNullOrEmpty(_uid))
           // {
           //     using (ISession session = SessionFactoryHelper.CreateSession())
           //     {
           //         mi = session.CreateQuery("from MemberInfo where  AccountId=:acc  ")
           //         .SetParameter("acc", _uid).UniqueResult<MemberInfo>();
           //         //  session.CreateCriteria<MemberInfo>().Add(Expression.Eq("AccountId",UserKit.GetUserAccount())).UniqueResult<MemberInfo>();

           //         if (mi == null)
           //         {
           //             return;
           //         }
           //     }
           // }
        }

        protected void tb_commit_Click(object sender, EventArgs e)
        {
            SaveUser();
            Response.Write("<script type='text/javascript'>alert('修改成功!');location.href='/MemberUser/ModifyUserInfo.aspx'</script>");
        }
    }
}
